package cz.acies.data.process;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

import cz.acies.data.DBConnector;
import cz.acies.data.UserContext;
import cz.acies.data.dao.UserLoginDAO;
import cz.acies.data.dao.UserPersonDAO;
import cz.acies.data.persist.ModelObject;
import cz.acies.data.persist.UserLogin;
import cz.acies.data.persist.UserPerson;
import cz.acies.utils.UIDGenerator;

public class UserProcessor extends AbstractProcessor
{
	public UserProcessor() {
	}

	@Override
	public String execute(Map<String, Object> params) {
		String data = "";
		String processId = params.containsKey("processId") ? (String) params.get("processId") : "UNKNOW";
		System.out.println("processId = "+processId);
		if (processId.equals("selectAll")) {
			//data = selectCurrentMonth();
		} else if (processId.equals("doAppend")) {
			//System.out.println("doAppend: " + params);
			//data = doAppend(params);
		} else if (processId.equals("doUpdate")) {
			//System.out.println("doUpdate: " + params);
			//data = doUpdate(params);
		} else if (processId.equals("doDelete")) {
			
		} else if (processId.equals("userLogin")) {
			data = isLogged(params);
		} else {
			data = "UNKNOW";
		}
		return data;
	}

	public String isLogged(Map<String, Object> params) {
		String data = "";
		try {
			UserLoginDAO dao = new UserLoginDAO(); 
			Map<String, String> map = new LinkedHashMap<String, String>();
			map.put("userId", ""); 
			
			UserLogin login = new UserLogin(null, params);
			login = dao.doLoad(login);
			String id = (login != null) ? login.getId() : null;
			if (id != null && id.startsWith("WX.L.")) {
				map.put("userId", ""+id);
				UserContext uCtx = new UserContext();
				uCtx.setWorkerId(id);
				uCtx.db(DBConnector.instance());
				uCtx.setLogin(login);
				request().getSession().setAttribute("uCtx", uCtx);
			}
			Gson gson = new Gson();
			data = "" + gson.toJson(map);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}

	public UserPerson refreshUserPerson(String id) {
		UserPerson person = null; 
		try {
			if (id == null || !id.startsWith("WX.R")) return null;
			return refreshUserPerson(new UserPerson(id));
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return person;
	}

	public UserPerson refreshUserPerson(UserPerson person) {
		try {
			String id = person.getId();
			UserPersonDAO dao = new UserPersonDAO();  
			person = dao.loadDeatail(id);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return person;
	}
	
	public String doAppend(Map<String, Object> params) {
		String data = "";
		try {
			UserPerson record = new UserPerson();
			record.setId(UIDGenerator.createUID("WX.R"));
			UserPersonDAO personDao = new UserPersonDAO();
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}
	
	public String doUpdate(Map<String, Object> params) {
		String data = "";
		try {
			System.out.println("UserProcessor.doUpdate().params: "+params);
			String id = (params.containsKey("id")) ? ""+params.get("id") : "";
			UserPersonDAO dao = new UserPersonDAO(); 
			List<ModelObject> records = dao.selectById("user_person", id, UserPerson.class);
			String processId = "doUpdate";
			UserPerson person = null;
			if (records.isEmpty()) {
				person = new UserPerson(UIDGenerator.createUID("WX.R"));
				processId = "doAppend";
			} else {
				person = (UserPerson) records.get(0);
			}
			person.setFirstName(params.containsKey("firstName") ? (String) params.get("firstName") : "");
			person.setLastName(params.containsKey("lastName") ? (String) params.get("lastName") : "");
			if (processId.equals("doAppend")) {
				dao.doAppend(person);
			} else if (processId.equals("doUpdate")) {
				dao.doUpdate(person);
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}

}
